package sixsigma;

import java.awt.Component;
import java.awt.Frame;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLEngineResult.Status;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Marc
 */
public class ZuordnungGUI extends javax.swing.JFrame {

    DbKomm dbk = new DbKomm();
    
    String[][] FR = null;
    String[] FR_m = null;
    
    String[][] DP = null;
    String[] DP_m = null;

    String ProjectID = null;
    String GroupID = null;

    public ZuordnungGUI(String ProjectID, String GroupID) {        
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.ProjectID = ProjectID;
        this.GroupID = GroupID;
        this.FR_m = getFR();
        this.DP_m = getDP();
        setLocationRelativeTo(null);
        initComponents();
        fillFRCombo();
        fillDPCombo();
        super.setLocationRelativeTo(null);
        setVisible(true);
    }

    // BEGIN Methoden

    private String[] getFR(){
        FR = getData("SELECT FR_ID, FR_TXT FROM fr f WHERE FR_PROJECT_ID = "+this.ProjectID+";");
        Vector<String> v = new Vector();

        String temp[] = new String[200];

        for(int i=0; i < FR.length; i++){
            temp[i] = FR[i][1] + " (" + FR[i][0] + ")";
        }

        for(int i=0; i < temp.length; i++){
            if(temp[i] == null){
                ;
            }else{
                v.add(temp[i]);
            }        
        }
        String[] ss = (String[])v.toArray(new String[v.size()]);
        return ss;
    }
    
    private String[] getFRGroup(){
        FR = getData("select FR_GROUP_ID, FR_GROUP_TEXT from fr_group");
        Vector<String> v = new Vector();

        String temp[] = new String[200];

        for(int i=0; i < FR.length; i++){
            temp[i] = FR[i][1] + " (" + FR[i][0] + ")";
        }

        for(int i=0; i < temp.length; i++){
            if(temp[i] == null){
                ;
            }else{
                v.add(temp[i]);
            }        
        }
        String[] ss = (String[])v.toArray(new String[v.size()]);
        return ss;
    }
    
    private String[] getDPGroup(){
        FR = getData("select DP_GROUP_ID, DP_GROUP_TXT from dp_group");
        Vector<String> v = new Vector();

        String temp[] = new String[200];

        for(int i=0; i < FR.length; i++){
            temp[i] = FR[i][1] + " (" + FR[i][0] + ")";
        }

        for(int i=0; i < temp.length; i++){
            if(temp[i] == null){
                ;
            }else{
                v.add(temp[i]);
            }        
        }
        String[] ss = (String[])v.toArray(new String[v.size()]);
        return ss;
    }

    private void fillFRCombo(){
        for(int i = 0; i<FR_m.length; i++){
           if(FR_m[i] == null){
               ;
           }else{
               jComboBox1.addItem(FR_m[i]);
           }
        }
    }

    private void fillDPCombo(){
        for(int i = 0; i<DP_m.length; i++){
            if(DP_m[i] == null){
                ;
            }else{
                jComboBox2.addItem(DP_m[i]);
            }
        }
    }


    private String[] getDP(){
        DP = getData("SELECT DP_ID, DP_TXT FROM dp d WHERE DP_PROJECT_ID = "+this.ProjectID+";");
        Vector<String> v = new Vector();
        
        String temp[] = new String[200];

        for(int i=0; i < DP.length; i++){
            temp[i] = DP[i][1] + " (" + DP[i][0] + ")";
        }

        for(int i=0; i < temp.length; i++){
            if(temp[i] == null){
                ;
            }else{
                v.add(temp[i]);
            }
        }
        String[] ss = (String[])v.toArray(new String[v.size()]);
        return ss;
    }

    private String getID(String Item){
        String ID="";
        if (Item!=null) ID = Item.substring(Item.lastIndexOf("(")+1, Item.lastIndexOf(")"));
        return ID;
    }

    private void FRanlegen(){
        String neuerWert = JOptionPane.showInputDialog("Bitte Namen für FR eingeben...");
                                                 
        String selectedFR = jComboBox1.getSelectedItem().toString();
        String selectedFR_ID = getID(selectedFR);

        if(neuerWert == null || neuerWert.length() == 0){
            JOptionPane.showMessageDialog(null,"Bitte geben Sie einen gültigen Wert ein!");
        }else{
        JFrame frame = new JFrame("Input Dialog Example 3");
        String[] FR = this.getFRGroup();
        String favoriteFR = (String) JOptionPane.showInputDialog(frame,"FR folgender Gruppe zuweisen","Gruppenzugehörigkeit",JOptionPane.QUESTION_MESSAGE,null,FR,FR[0]);
        String id = getID(favoriteFR);
        if (id.length()>0 && favoriteFR != null){

            Status.setText("FR erfolgreich angelegt");
            
            setData("INSERT INTO fr (FR_TXT, FR_GROUP_ID, FR_PROJECT_ID) VALUES ('"+neuerWert+"', '"+id+"', '"+this.ProjectID+"')");
            this.FR_m = getFR();
            jComboBox1.setModel(new DefaultComboBoxModel(this.FR_m));
        }
        }
    }

    private void DPanlegen(){
        String neuerWert = JOptionPane.showInputDialog("Bitte Namen für DP eingeben...");
                                                 
        String selectedDP = jComboBox2.getSelectedItem().toString();
        String selectedDP_ID = getID(selectedDP);

        if(neuerWert == null || neuerWert.length() == 0){
            JOptionPane.showMessageDialog(null,"Bitte geben Sie einen gültigen Wert ein!");
        }else{
        JFrame frame = new JFrame("Input Dialog Example 3");
        String[] DP = this.getDPGroup();
        String favoriteDP = (String) JOptionPane.showInputDialog(frame,"DP folgender Gruppe zuweisen","Gruppenzugehörigkeit",JOptionPane.QUESTION_MESSAGE,null,DP,DP[0]);
        String id = getID(favoriteDP);
        if (id.length()>0 && favoriteDP != null){

            Status.setText("DP erfolgreich angelegt");
            
            setData("INSERT INTO dp (DP_TXT, DP_GROUP_ID, DP_PROJECT_ID) VALUES ('"+neuerWert+"', '"+id+"', '"+this.ProjectID+"')");
            this.DP_m = getDP();
            jComboBox2.setModel(new DefaultComboBoxModel(this.DP_m));
        }
        }
    }

    private void relationSpeichern(){
        String selectedFR = jComboBox1.getSelectedItem().toString();
        String selectedFR_ID = getID(selectedFR);
        String selectedDP = jComboBox2.getSelectedItem().toString();
        String selectedDP_ID = getID(selectedDP);

        if(getData("SELECT FR_DP_REL_ID FROM fr_dp_rel f WHERE FR_ID="+selectedFR_ID+ " AND DP_ID="+selectedDP_ID+";").length!=0){
            Status.setText("Relation existiert bereits.");
        }
        else{
            setData("INSERT INTO fr_dp_rel (FR_ID, DP_ID, CRIT_ID) VALUES ('"+selectedFR_ID+"', '"+selectedDP_ID+"', 2)");
            Status.setText("Erfolgreich gespeichert!");
        }
    }

    // END Methoden

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        Status = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        refreshpic = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Abhängigkeit definieren");
        setResizable(false);

        jLabel1.setText("+");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Abhängigkeiten definieren");

        jButton2.setText("FR löschen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("DP löschen");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("FR anlegen");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("DP anlegen");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton7.setText("FR ändern");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("DP ändern");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        refreshpic.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lib/aktualisieren.gif"))); // NOI18N
        refreshpic.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                refreshpicMouseClicked(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lib/speichern.gif"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });

        jLabel4.setText("Relation speichern");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(jButton4))
                                .addGap(129, 129, 129)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                                .addComponent(refreshpic))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(Status)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                                .addComponent(jLabel4)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(33, 33, 33))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(refreshpic))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton8)
                    .addComponent(jButton7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Status)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String selectedItem = jComboBox1.getSelectedItem().toString();
        if (jComboBox1.getItemCount()>1){
            String ID = getID(selectedItem);
            setData("DELETE FROM fr WHERE FR_ID = "+ ID);
            Status.setText("FR erfolgreich gelöscht");
            this.FR_m = getFR();
            jComboBox1.setModel(new DefaultComboBoxModel(this.FR_m));
        }else{
            JOptionPane.showMessageDialog(null,"Der einzigste FR des Projektes kann nicht gelöscht werden. Bitte stattdessen den FR ändern.");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String selectedItem = jComboBox2.getSelectedItem().toString();
        if (jComboBox1.getItemCount()>1){
            String ID = getID(selectedItem);
            setData("DELETE FROM dp WHERE DP_ID = "+ ID);
            Status.setText("DP erfolgreich gelöscht");
            this.DP_m = getDP();
            jComboBox2.setModel(new DefaultComboBoxModel(this.DP_m));
        }else{
            JOptionPane.showMessageDialog(null,"Der einzigste DP des Projektes kann nicht gelöscht werden! Bitte stattdessen den DP ändern.");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        FRanlegen();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        DPanlegen();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        String selectedFR = jComboBox1.getSelectedItem().toString();
        String selectedFR_ID = getID(selectedFR);

        String neuerWert = JOptionPane.showInputDialog("Bitte neuen Namen eingeben...");
        if(neuerWert == null || neuerWert.length() == 0){
            JOptionPane.showMessageDialog(null,"Bitte geben Sie einen gültigen Wert ein!");
        }else{
        JFrame frame = new JFrame("Input Dialog Example 3");
        String[] FR = this.getFRGroup();
        String favoriteFR = (String) JOptionPane.showInputDialog(frame,"FR folgender Gruppe zuweisen","Gruppenzugehörigkeit",JOptionPane.QUESTION_MESSAGE,null,FR,FR[0]);
        String id = getID(favoriteFR);
        if (id.length()>0 && favoriteFR != null){
            setData("UPDATE FR SET FR_TXT = '"+neuerWert+"' WHERE FR_ID = " + selectedFR_ID);
            setData("UPDATE FR SET FR_GROUP_ID = '"+id+"' WHERE FR_ID = " + selectedFR_ID);

            Status.setText("FR erfolgreich geändert");
        }
        }

        this.FR_m = getFR();
        jComboBox1.setModel(new DefaultComboBoxModel(this.FR_m));
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        String selectedDP = jComboBox2.getSelectedItem().toString();
        String selectedDP_ID = getID(selectedDP);

        String neuerWert = JOptionPane.showInputDialog("Bitte neuen Namen eingeben...");
        if(neuerWert == null || neuerWert.length() == 0){
            JOptionPane.showMessageDialog(null,"Bitte geben Sie einen gültigen Wert ein!");
        }else{
        JFrame frame = new JFrame("Input Dialog Example 3");
        String[] DP = this.getDPGroup();
        String favoriteDP = (String) JOptionPane.showInputDialog(frame,"DP folgender Gruppe zuweisen","Gruppenzugehörigkeit",JOptionPane.QUESTION_MESSAGE,null,DP,DP[0]);
        String id = getID(favoriteDP);
        if (id.length()>0 && favoriteDP != null){
            setData("UPDATE DP SET DP_TXT = '"+neuerWert+"' WHERE DP_ID = " + selectedDP_ID);
            setData("UPDATE DP SET DP_GROUP_ID = '"+id+"' WHERE DP_ID = " + selectedDP_ID);

            Status.setText("DP erfolgreich geändert");
        }
        }

        this.DP_m = getDP();
        jComboBox2.setModel(new DefaultComboBoxModel(this.DP_m));
    }//GEN-LAST:event_jButton8ActionPerformed

    private void refreshpicMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_refreshpicMouseClicked
        this.FR_m = getFR();
        jComboBox1.setModel(new DefaultComboBoxModel(this.FR_m));

        this.DP_m = getDP();
        jComboBox2.setModel(new DefaultComboBoxModel(this.DP_m));

        Status.setText("");
    }//GEN-LAST:event_refreshpicMouseClicked

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        relationSpeichern();
    }//GEN-LAST:event_jLabel3MouseClicked

    private String[][] getData(String SQL){

        try {dbk.open();}catch(Exception ex){
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        String[][] ergebnis = dbk.einfachArrayAuslesen(SQL);
        try {dbk.close();}catch(Exception ex){
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ergebnis;
    }

    private void setData(String SQL){
        try {dbk.open();}catch(Exception ex){
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        dbk.execute(SQL);
        try {dbk.close();}catch(Exception ex){
            Logger.getLogger(ZuordnungGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Status;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel refreshpic;
    // End of variables declaration//GEN-END:variables
}
